package com.saifan.wyy_ov.model.impl;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.ui.user.LoginActivity;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.model.interfaces.IGetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.utils.CommonUtils;
import com.saifan.wyy_ov.utils.DialogUtil;
import com.saifan.wyy_ov.utils.GsonUtils;
import com.saifan.wyy_ov.utils.LogUtil;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * @author WangLi
 * @version V1.0
 * @date 2014年7月24日 上午10:09:29
 * @Description
 */

public class GetData implements IGetData {
    private int timeOut = 15000;

    @Override
    public void getData(String method, RequestParams params, RequestCallBack<String> requestCallBack) {
        HttpUtils http = new HttpUtils();
        http.configSoTimeout(timeOut);
        http.send(HttpRequest.HttpMethod.POST, Api.getUrlRoot()  + method, params, requestCallBack);
    }

    /**
     * 获取数据(Json方式)
     *
     * @param context      上下文
     * @param method       接口方法
     * @param jsonClass    参数
     * @param tipString    提示Dialog显示文字
     * @param responseData 返回数据
     */
    @Override
    public void getDataJson(final Context context, final String method, Object jsonClass, final String tipString, final ResponseData responseData) {
        if (!CommonUtils.isNetWorkConnected(context)) {
            ToastUtil.showToast(context, context.getString(R.string.network_null));
            DialogUtil.closeProgressDialog();
            return;
        }
        HttpUtils http = new HttpUtils();
        http.configSoTimeout(timeOut);
        RequestParams requestParams = new RequestParams();
        requestParams.setContentType("application/json");
        try {
            requestParams.setBodyEntity(new StringEntity(GsonUtils.toJson(jsonClass), "UTF-8"));

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        http.send(HttpRequest.HttpMethod.POST, Api.getUrlRoot()  + method, requestParams,
                new RequestCallBack<String>() {

                    @Override
                    public void onStart() {
                        super.onStart();
                        if (TextUtils.isEmpty(Api.getUrlIp())){
                            ToastUtil.showToast(context, "请设置授权项目");
                            responseData.onFailed("code:" + 500);
                            return;
                        }
                        if (!StringUtil.isNullOrEmpty(tipString))
                            DialogUtil.showProgressDialog(context, tipString);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        if (!StringUtil.isNullOrEmpty(responseInfo.result)) {
                            try {
                                JSONObject jsonObject = new JSONObject(responseInfo.result.toString());
                                String code = jsonObject.getString("Code");
                                if (code.equals("200")) {
                                    DialogUtil.closeProgressDialog();
                                    responseData.onSuccess(jsonObject.getString("Content"));
                                } else if (code.equals("203")) {
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, context.getString(R.string.login_error_tip));
                                    if (method== Api.Customer.LOGIN){
                                        context.startActivity(new Intent(context, LoginActivity.class));
                                    }

                                } else if (code.equals("204")) {
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, "用户名已存在 !");
                                } else if (code.equals("202")) {
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, "保存失败，信息未做修改 !");
                                }else if (code.equals("209")){
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, "该功能暂时不可用");
                                } else {
                                    LogUtil.e(code);
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, "服务器出错了");
                                    responseData.onFailed("服务器出错了");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {

                            DialogUtil.closeProgressDialog();
                            ToastUtil.showToast(context, "服务器出错了");
                            responseData.onFailed("服务器出错了");
                            LogUtil.e("ServerError");
                        }

                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        DialogUtil.closeProgressDialog();
                        ToastUtil.showToast(context, context.getString(R.string.network_error));
                        responseData.onFailed(msg);
                    }
                });
    }


    /**
     * 获取数据(Json方式)
     *
     * @param context      上下文
     * @param method       接口方法
     * @param jsonObject   参数
     * @param tipString    提示Dialog显示文字
     * @param responseData 返回数据
     */
    @Override
    public void getDataJson(final Context context, String method, JSONObject jsonObject, final String tipString, final ResponseData responseData) {
        if (!CommonUtils.isNetWorkConnected(context)) {
            ToastUtil.showToast(context, context.getString(R.string.network_null));
            return;
        }
        HttpUtils http = new HttpUtils();
        http.configSoTimeout(timeOut);
        RequestParams requestParams = new RequestParams();
        requestParams.setContentType("application/json");
        try {
            requestParams.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        http.send(HttpRequest.HttpMethod.POST, Api.getUrlRoot()  + method, requestParams,
                new RequestCallBack<String>() {

                    @Override
                    public void onStart() {
                        super.onStart();
                        if (TextUtils.isEmpty(Api.getUrlIp())){
                            ToastUtil.showToast(context, "请设置授权项目");
                            responseData.onFailed("code:" + 500);
                            return;
                        }

                        if (!StringUtil.isNullOrEmpty(tipString))
                            DialogUtil.showProgressDialog(context, tipString);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        if (!StringUtil.isNullOrEmpty(responseInfo.result)) {
                            try {
                                JSONObject jsonObject = new JSONObject(responseInfo.result.toString());
                                String code = jsonObject.getString("Code");
                                if (code.equals("200")) {
                                    DialogUtil.closeProgressDialog();
                                    responseData.onSuccess(jsonObject.getString("Content"));
                                }
                                else if (code.equals("201")) {
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, "数据错误!");
                                }
                                else if (code.equals("203")) {
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, context.getString(R.string.login_error_tip));
                                } else if (code.equals("204")) {
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, "用户名已存在 !");
                                } else if (code.equals("205")) {
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, "信息已经提交过了 !");
                                } else if (code.equals("202")) {
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, "保存失败，信息未做修改 !");
                                }
                                else if (code.equals("206")) {
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, "用户或证件号码错误！");
                                }
                                else {
                                    LogUtil.e(code);
                                    ToastUtil.showToast(context, "服务器出错了");
                                    DialogUtil.closeProgressDialog();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            LogUtil.e("ServerError");
                        }

                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        DialogUtil.closeProgressDialog();
                        ToastUtil.showToast(context, context.getString(R.string.network_error));
                        responseData.onFailed(msg);
                    }
                });
    }

    @Override
    public void getDataJson(final Context context, String method, RequestParams params, final String tipString, final ResponseData responseData) {
        if (!CommonUtils.isNetWorkConnected(context)) {
            ToastUtil.showToast(context, context.getString(R.string.network_null));
            DialogUtil.closeProgressDialog();
            return;
        }
        HttpUtils http = new HttpUtils();
        http.configSoTimeout(timeOut);
        http.send(HttpRequest.HttpMethod.POST, Api.getUrlRoot()  + method, params,
                new RequestCallBack<String>() {

                    @Override
                    public void onStart() {
                        super.onStart();
                        if (TextUtils.isEmpty(Api.getUrlIp())){
                            ToastUtil.showToast(context, "请设置授权项目");
                            responseData.onFailed("code:" + 500);
                            return;
                        }

                        if (!StringUtil.isNullOrEmpty(tipString))
                            DialogUtil.showProgressDialog(context, tipString);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        if (!StringUtil.isNullOrEmpty(responseInfo.result)) {
                            try {
                                JSONObject jsonObject = new JSONObject(responseInfo.result.toString());
                                String code = jsonObject.getString("Code");
                                if (code.equals("200")) {
                                    DialogUtil.closeProgressDialog();
                                    responseData.onSuccess(jsonObject.getString("Content"));
                                } else if (code.equals("203")) {
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, context.getString(R.string.login_error_tip));
                                } else if (code.equals("204")) {
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, "用户名已存在 !");
                                } else if (code.equals("205")) {
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, "信息已经提交过了 !");
                                } else if (code.equals("202")) {
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, "保存失败，信息未做修改 !");
                                } else {
                                    LogUtil.e(code);
                                    ToastUtil.showToast(context, "服务器出错了");
                                    DialogUtil.closeProgressDialog();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            LogUtil.e("ServerError");
                        }

                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        DialogUtil.closeProgressDialog();
                        ToastUtil.showToast(context, context.getString(R.string.network_error));
                        responseData.onFailed(msg);
                    }
                });
    }

    @Override
    public void httpGetDataJson(final Context context, String method, final String tipString, final ResponseData responseData) {
        if (!CommonUtils.isNetWorkConnected(context)) {
            ToastUtil.showToast(context, context.getString(R.string.network_null));
            return;
        }
        HttpUtils http = new HttpUtils();
        http.configSoTimeout(timeOut);
        http.send(HttpRequest.HttpMethod.GET, Api.getUrlRoot()  + method,
                new RequestCallBack<String>() {

                    @Override
                    public void onStart() {
                        super.onStart();
                        if (TextUtils.isEmpty(Api.getUrlIp())){
                            ToastUtil.showToast(context, "请设置授权项目");
                            responseData.onFailed("code:" + 500);
                            return;
                        }
                        if (!StringUtil.isNullOrEmpty(tipString))
                            DialogUtil.showProgressDialog(context, tipString);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        if (!StringUtil.isNullOrEmpty(responseInfo.result)) {
                            try {
                                JSONObject jsonObject = new JSONObject(responseInfo.result.toString());
                                String code = jsonObject.getString("Code");
                                if (code.equals("200")) {
                                    DialogUtil.closeProgressDialog();
                                    responseData.onSuccess(jsonObject.getString("Content"));
                                } else if (code.equals("203")) {
                                    DialogUtil.closeProgressDialog();
                                    ToastUtil.showToast(context, context.getString(R.string.login_error_tip));
                                } else {
                                    LogUtil.e(code);
                                    ToastUtil.showToast(context, "服务器出错了");
                                    DialogUtil.closeProgressDialog();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            LogUtil.e("ServerError");
                        }

                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        DialogUtil.closeProgressDialog();
                        ToastUtil.showToast(context, context.getString(R.string.network_error));
                        responseData.onFailed(msg);
                    }
                });
    }


}
